package jm.nj.demo11main.demo03;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * try...cat ch
 *
 * try{
 *       possible exception code
 * }catch(出现异常的对象 变量名){
 *       处理异常的方式
 * }finally{
 *       资源的释放
 * }
 * */
public class Demo03CopyFile {
    public static void main(String[] args) {
        //1.//把流的变量提取处理提高作用域
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:\\0002.jpg");
            fos = new FileOutputStream("F:\\02.jpg");
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (Exception e) {
            System.out.println(e);
            throw new RuntimeException("复制文件失败");
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            }catch (IOException e){
                System.out.println(e);

                throw new RuntimeException("复制失败");
            }finally {
                try {
                    if (fos!=null){
                        fos.close();
                    }
                }catch (IOException e){
                    System.out.println(e);
                    throw new RuntimeException("yc");
                }
            }

        }

    }
}
